﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataAccessLayer;
using VCN.App.DataTransferObjects;

namespace VCN.admin
{
    public partial class ProductList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                LoadData();
                LoadDataToDdl();
                Page.Title = "Danh sách sản phẩm";
            }

        }
        private void LoadData()
        {
            ProductDAO v_ProductDAO = new ProductDAO();
            DataTable v_dt_Product = new DataTable();
            v_dt_Product = v_ProductDAO.selectAll2Dt();
            if (v_dt_Product.Rows.Count == 0)
            {
                m_lbl_mess.Text = "Chưa có dữ liệu !";
                m_lbl_mess.Visible = true;
            }
            else
            {
                if (m_lbl_mess.Text!="Có sản phẩm liên quan, không thể xóa được !") m_lbl_mess.Visible = false;
            }
            dgv_Product.DataSource = v_dt_Product;
            dgv_Product.DataBind();

        }
        private void LoadDataToDdl()
        {
            CategoriesDAO v_CategoryDAO = new CategoriesDAO();
            DataTable v_dt = v_CategoryDAO.selectAll2Dt();
            ddl_Category.Items.Add(new ListItem("Tất cả", "0"));
            for (int v_i = 0; v_i < v_dt.Rows.Count; v_i++)
            {
                ddl_Category.Items.Add(new ListItem(v_dt.Rows[v_i]["CATEGORY_NAME"].ToString(),v_dt.Rows[v_i]["Id"].ToString()));
            }
        }
        protected void gridProduct_Delete(object sender, GridViewDeleteEventArgs e)
        {
            ProductDAO v_ProductDAO = new ProductDAO();
            VCN.App.DataTransferObjects.Product v_Product=new VCN.App.DataTransferObjects.Product();
            v_Product.Id = int.Parse(dgv_Product.DataKeys[e.RowIndex].Value.ToString());
            if (!v_ProductDAO.delete(v_Product))
            {
                m_lbl_mess.Text = "Có sản phẩm liên quan, không thể xóa được !";
                m_lbl_mess.Visible = true;
            }
            else
            {
                m_lbl_mess.Visible = false;
            }
            this.LoadData();
        }

        protected void gridProduct_Edit(object sender, GridViewEditEventArgs e)
        {
            string v_id = dgv_Product.DataKeys[e.NewEditIndex].Value.ToString();
            Response.Redirect(string.Format("ProductManagement.aspx?Id={0}", v_id));
        }

        protected void dgv_Product_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                dgv_Product.PageIndex = e.NewPageIndex;
                ProductDAO v_ProductDAO = new ProductDAO();
                dgv_Product.DataSource = v_ProductDAO.selectAll2Dt();
                dgv_Product.DataBind();
            }
            catch (Exception ip_e)
            {
                ip_e.ToString();
            }
        }

        protected void Search(object sender, EventArgs e)
        {
            ProductDAO v_ProductDAO = new ProductDAO();
            DataTable v_dt_Product = new DataTable();
            v_dt_Product = v_ProductDAO.searchProduct(int.Parse(ddl_Category.SelectedValue), string.Concat("%", txt_KeyWord.Text, "%")); ;
            if (v_dt_Product.Rows.Count == 0)
            {
                m_lbl_mess.Text = "Không có kết quả nào liên quan đến từ khóa trên !";
                m_lbl_mess.Visible = true;
            }
            else
            {
                m_lbl_mess.Visible = false;
            }
            dgv_Product.DataSource = v_dt_Product;
            dgv_Product.DataBind();
        }
        public string GetCategoryName(object ip_int_Categories)
        {
            // string CategoryName = "";
            CategoriesDAO v_CategoriesDAO = new CategoriesDAO();
            return v_CategoriesDAO.SelectById( int.Parse( ip_int_Categories.ToString())).CategoryName;
        }

        protected void ddl_Category_SelectedIndexChanged(object sender, EventArgs e)
        {
            ProductDAO v_ProductDAO = new ProductDAO();
            dgv_Product.DataSource = v_ProductDAO.searchProduct(int.Parse(ddl_Category.SelectedValue), string.Concat(""));
            dgv_Product.DataBind();
        }

        protected void ThemMoi_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddProduct.aspx");
        }
    }
}
